package fit.conceptmap.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fit.conceptmap.beans.JsonBean;
import fit.conceptmap.dao.IDrawDao;
import fit.conceptmap.entities.Concept;
import fit.conceptmap.entities.Conceptmap;
import fit.conceptmap.entities.Link;
import fit.conceptmap.entities.Node;
import fit.conceptmap.entities.Relationship;
import fit.conceptmap.services.IDrawService;
import fit.conceptmap.utils.DrawUtils;

@Service
public class DrawServiceImpl implements IDrawService {

	@Autowired IDrawDao drawDao;
	
	/**
	 * Step 1: Add Concept
	 * Step 2: Add Node
	 * Step 3: Add Relationship
	 * Step 4: Add Link
	 */
	@Override
	public boolean addDraw(String username, JsonBean json) {
		Map<Integer, Concept> concepts = new HashMap<Integer, Concept>();
		List<String > relationID = new ArrayList<String>();
		boolean addConcept = drawDao.addConcept(concepts, json);
		boolean addNode = drawDao.addNode(concepts, json, username);
		boolean addRelationship = drawDao.addRelationship(relationID, concepts, json, username, json.getIdConceptMaps());
		boolean addLink = drawDao.addLink(relationID, json, username);
		return addConcept && addNode && addRelationship && addLink ;
	}

	@Override
	public boolean addConceptMaps(String username, JsonBean json) {
		return drawDao.addConceptMaps(username, json);
	}

	@Override
	public List<JsonBean> getListConcepmapByUsername(String username) {
		List<Conceptmap> lst = drawDao.getListConcepmapByUsername(username);
		List<JsonBean> result = new ArrayList<JsonBean>();
		JsonBean json;
		for (Conceptmap conceptmap : lst) {
			json = new JsonBean();
			json.setDescription(conceptmap.getDescription());
			json.setNameConceptMaps(conceptmap.getName());
			json.setIdConceptMaps(conceptmap.getIdConceptMaps());
			json.setDateCreate(conceptmap.getDateCreate().toString());
			result.add(json);
		}
		return result;
	}

	/**
	 * Step 1: Get List<Relationship> by conceptMapsId
	 * Step 2: Get List<Concept> by list relationship id 
	 * Step 3: Get List<Node> by concept
	 * Step 4: Get List<Link> by list relationship id
	 */
	@Override
	public JsonBean getJsonBean(String conceptMapsId) {
		// Step 1
		List<Relationship> relationships = drawDao.getListRelationshipByConceptMapsId(conceptMapsId);
		List<String> relationId = new ArrayList<String>();
		for (int i = 0; i < relationships.size(); i++) {
			relationId.add(relationships.get(i).getIdrelationship());
		}
		// Step 2
		List<Concept> concepts =  drawDao.getListConceptByConceptMapId(relationId);
		List<String> conceptsId = new ArrayList<String>();
		for (int i = 0; i < concepts.size(); i++) {
			conceptsId.add(concepts.get(i).getIdConcept());
		}
		// Step 3
		List<Node> nodes = drawDao.getListNodeByListConceptId(conceptsId);
		// Step 4
		List<Link> links = drawDao.getListLinkByListRelationshipId(relationId);
		
		JsonBean result = DrawUtils.parseConceptMapToJsonBean(nodes, links);
		return result;
	}

}
